/*
 * @Author: your name
 * @Date: 2021-06-03 11:00:15
 * @LastEditTime: 2021-06-03 16:50:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_island_api\app\models\favor.js
 */
const { Model, Sequelize, Op } = require("sequelize");
const { sequelize } =  require('@core/mysqlDB')
const { Art } = require('./art')

class Favor extends Model{
    // 业务表
    static async like(art_id, type, uid) {
        const favor = await Favor.findOne({
            where: {
                art_id,
                type,
                uid
            }
        })
        if (favor) {
            // 用户已点赞
            throw new global.errs.LikeError()
        }
        // 进入事务
        return sequelize.transaction(async t => {
            await Favor.create({
                art_id,
                type,
                uid
            }, { transaction: t })
            const art = await Art.getData(art_id, type)
            // 对原有的数值加1操作
            await art.increment('fav_nums', { by: 1, transaction: t})
        })
        // 1.添加记录
        // 2.classic
        // 数据库事务,
        
        // 保证数据一致性
        // ACID 原子性，隔离性，一致性，
    }

    static async disLike(art_id, type, uid) {
        const favor = await Favor.findOne({
            where: {
                art_id,
                type,
                uid
            }
        })
        if (!favor) {
            throw new global.errs.DislikeError()
        }
        
        return sequelize.transaction(async t => {
            await favor.destroy({
                force: false, // false: 软删除, true ,物理删除
                transaction: t
            })
            const art = await Art.getData(art_id, type)
            // 减法操作
            await art.decrement('fav_nums', { by: 1, transaction: t})
        })
    }

    static async userLikeIt(art_id, type, uid) {
        const favor = await Favor.findOne({
            where: {
                uid,
                art_id,
                type
            }
        })
        return favor ? true : false
    }

    static async getMyClassicFavors(uid) {

        const arts = await Favor.findAll({
            where: {
                uid,
                type: {
                    [Op.not]: 400
                }
            }
        })

        if (!arts) {
            throw new global.errs.NotFound()
        }
        return await Art.getList(arts)
    }

    static async getBookFavor(uid, bookId) {
      const favorNums = await Favor.count({
        where: {
          art_id: bookId,
          type: 400
        }
      })
      const myFavor = await Favor.findOne({
        where: {
          art_id: bookId,
          uid,
          type: 400
        }
      })
      return {
        fav_nums: favorNums,
        like_status: myFavor ? 1 : 0
      }
    }
}

Favor.init({
    uid: Sequelize.INTEGER,
    art_id: Sequelize.INTEGER,
    type: Sequelize.INTEGER
},{
    sequelize,
    tableName: 'favor'
})


module.exports = {
    Favor
}